package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class EditProfileActivity extends AppCompatActivity {
    private ImageView profileImage;
    private EditText etUsername, etBio;
    private Button btnChangeAvatar, btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);

        // 初始化视图
        profileImage = findViewById(R.id.profile_image);
        etUsername = findViewById(R.id.et_username);
        etBio = findViewById(R.id.et_bio);
        btnChangeAvatar = findViewById(R.id.btn_change_avatar);
        btnSave = findViewById(R.id.btn_save);

        // 设置点击监听器
        btnChangeAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(EditProfileActivity.this, "暂不支持修改头像", Toast.LENGTH_SHORT).show();
            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveProfile();
            }
        });
    }

    private void saveProfile() {
        String username = etUsername.getText().toString().trim();
        String bio = etBio.getText().toString().trim();

        if (username.isEmpty()) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 保存到SharedPreferences
        UserPrefs.saveUserCredentials(this, username, UserPrefs.getCurrentPassword(this));

        // 设置返回结果
        Intent resultIntent = new Intent();
        resultIntent.putExtra("new_username", username);
        setResult(RESULT_OK, resultIntent);

        Toast.makeText(EditProfileActivity.this, "资料保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }
}